package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description NursingPlanController
 * @Author Peng Hongyan
 * @Date 2025-08-30
 */
@RestController
@RequestMapping("/nursing")
@Api(tags = "护理计划管理相关接口")
public class NursingPlanController extends BaseController {
    @Autowired
    public NursingPlanService nursingPlanService;

    @GetMapping("/plan")
    @ApiOperation("获取所有护理计划")
    public ResponseResult<List<NursingPlanVo>> getAll() {
        List<NursingPlanVo> nursingPlanVoList = nursingPlanService.getAll();
        return success(nursingPlanVoList);
    }


    @PostMapping("/plan")
    @ApiOperation("添加护理计划")
    public ResponseResult addNursingPlan(@RequestBody NursingPlanVo nursingPlanVo) {
        nursingPlanService.addNursingPlan(nursingPlanVo);
        return success();
    }

    @GetMapping("/plan/search")
    @ApiOperation("分页条件查询护理计划")
    public ResponseResult<PageResponse<NursingPlanVo>> getByPage(
            @ApiParam(value = "页码")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
            @ApiParam(value = "每页数量")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @ApiParam(value = "护理计划名称")
            @RequestParam(value = "name", required = false) String planName,
            @ApiParam(value = "护理计划状态")
            @RequestParam(value = "status", required = false) Integer status
    ) {
        PageResponse<NursingPlanVo> nursingPlanVoList = nursingPlanService.getByPage(page, pageSize, planName, status);
        return ResponseResult.success(nursingPlanVoList);
    }

    @GetMapping("/plan/{id}")
    @ApiOperation("根据id查询护理计划")
    public ResponseResult<NursingPlanVo> findNursingPlanById(@PathVariable Integer id) {
        NursingPlanVo nursingPlanVo = nursingPlanService.findNursingPlanById(Long.valueOf(id));
        return ResponseResult.success(nursingPlanVo);
    }

    @PutMapping("/plan/{id}")
    @ApiOperation("更新护理计划")
    public ResponseResult updateNursingPlan(@PathVariable Integer id, @RequestBody NursingPlanVo nursingPlanVo) {
        nursingPlanVo.setId(Long.valueOf(id));
        nursingPlanVo.getProjectPlans().forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(id);
        });
        nursingPlanService.updateNursingPlan(nursingPlanVo);
        return success();
    }

    @DeleteMapping("/plan/{id}")
    @ApiOperation("删除护理计划")
    public ResponseResult deleteNursingPlan(@PathVariable Integer id) {
        nursingPlanService.deleteNursingPlan(Long.valueOf(id));
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("更新护理计划状态")
    public ResponseResult updateStatus(
            @ApiParam(value = "护理计划id")
            @PathVariable Integer id,
            @ApiParam(value = "护理计划状态")
            @PathVariable Integer status) {
        nursingPlanService.updateStatus(Long.valueOf(id), status);
        return success();
    }

}
